package com.jcdd.controller;

import com.jcdd.mapper.GoodsDetailMapper;
import com.jcdd.mapper.UserMapper;
import com.jcdd.object.db.GoodsDetail;
import com.jcdd.object.front.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// @CrossOrigin("*")
@RestController
@RequestMapping("/home")
@Slf4j
public class HomeController {

    @Autowired
    GoodsDetailMapper goodsDetailMapper;

    @Autowired
    UserMapper userMapper;

    @GetMapping("/firstGoods")
    public Result firstGoods(int region, int currentPage, int pageSize) {
        // 跳过 offset条数据
        int offset = (currentPage - 1) * pageSize;
        List<HomeFirstGoodsFO> result = goodsDetailMapper.selectFirstByRegion(region, offset, pageSize);
        return Result.success(result);
    }

    @GetMapping("/vipGoods")
    public Result vipGoods(int region, int currentPage, int pageSize) {
        // 跳过 offset条数据
        int offset = (currentPage - 1) * pageSize;
        List<HomeFirstGoodsFO> result = goodsDetailMapper.selectVipByRegion(region, offset, pageSize);
        return Result.success(result);
    }

    @GetMapping("/popularityGoods")
    public Result popularityGoods(int region, int currentPage, int pageSize) {
        // 跳过 offset条数据
        int offset = (currentPage - 1) * pageSize;
        List<HomeFirstGoodsFO> result = goodsDetailMapper.selectPopularityByRegion(region, offset, pageSize);
        return Result.success(result);
    }

    @GetMapping("/goods")
    public  Result goods(String id){
        long longId = Long.parseLong(id);
        HomeGoodsDetailFO homeGoodsDetailFO = goodsDetailMapper.getGoodsDetailById(longId);
        long sellerId = Long.parseLong(homeGoodsDetailFO.getSellerId());
        OwnUserInfoFO ownUserInfo = userMapper.getOwnUserInfo(sellerId);
        String chatUrl = userMapper.selectChatUrlByUserId(sellerId);
        HomeGoodsFO homeGoodsFO = new HomeGoodsFO(homeGoodsDetailFO, ownUserInfo,chatUrl);
        log.info(homeGoodsFO.toString());
        return Result.success(homeGoodsFO);
    }

    @GetMapping("/search")
    public Result search(int region, int currentPage, int pageSize, String content) {
        // 跳过 offset条数据
        int offset = (currentPage - 1) * pageSize;
        List<HomeFirstGoodsFO> result = goodsDetailMapper.searchByRegion(region, offset, pageSize, content);
        return Result.success(result);
    }

}
